package cn.passlink.bletoothchat.Tools.Location;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;

import java.util.List;

public class WifiLocationHelper {
    private Context context;
    private WifiManager wifiManager;
    private boolean isScanning = false;
    public boolean isWifiEnabled() {
        return wifiManager.isWifiEnabled();
    }

    public WifiLocationHelper(Context context) {
        this.context = context;
        wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    }

    public void startScan() {
        if (!isScanning) {
            isScanning = true;
            context.registerReceiver(wifiScanReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            wifiManager.startScan();
        }
    }

    public void stopScan() {
        if (isScanning) {
            isScanning = false;
            context.unregisterReceiver(wifiScanReceiver);
        }
    }

    private final BroadcastReceiver wifiScanReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {
                @SuppressLint("MissingPermission")
                List<ScanResult> scanResults = wifiManager.getScanResults();
                // 在这里处理获取到的 Wi-Fi 扫描结果
                // 根据扫描结果进行位置定位或位置匹配逻辑
            }
        }
    };
}